<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Core_Lib_Page.php");
require_once("../class/Common_Function.php");

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 || $userRole == 3){
	//跟单员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//获取form值
$doFlag = $_POST["doFlag"];


if($doFlag == "addProcess"){
	//订单进展
	$orderId = $_POST["orderId"];
	$processStr = $_POST["process_".$orderId];
	$processStr = "GD:".$processStr;
	$sql = "insert into orderprocess(order_id,order_process) values ($orderId,'$processStr')";
	$db->Execute($sql);
}


//分页设置
$list_rows = 3; //每页显示的记录数
if($doFlag == "toList_paid"){
	$sql_list = "SELECT COUNT(*) as total_rows  from orders o where o.order_status in (20,30,40) and  tracking_flag = 1  "; //已付款	
}else if($doFlag == "toList_all"){
	$sql_list = "SELECT COUNT(*) as total_rows  from orders o where   tracking_flag = 1  "; //全部	
}else{
	$sql_list = "SELECT COUNT(*) as total_rows  from orders o where o.order_status = 10 and  tracking_flag = 1  "; //没付款
}
$result = $db->Execute($sql_list);
if (!empty($result)) {
	$totalRows = $result->fields['total_rows'];
}
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => !empty($_POST['nowPage']) ? $_POST['nowPage'] : 1 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;


//显示列表
if($doFlag == "toList_paid"){
	$sql = "SELECT * from orders o where  o.order_status in (20,30,40) and tracking_flag = 1 order by pay_time desc limit $limit_m,$limit_n"; //已付款	
}else if($doFlag == "toList_all"){
	$sql = "SELECT * from orders o where   tracking_flag = 1 order by modify_time desc limit $limit_m,$limit_n"; //全部	
}else{
	$sql = "SELECT * from orders o where  o.order_status = 10 and tracking_flag = 1 order by modify_time desc limit $limit_m,$limit_n"; //没付款
}
$result = $db->Execute($sql);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "跟单列表"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="listForm" action="../tracking/trackingList.php" method="post">
<div id="container" >	

<div class="grayTable">	
<?php
$toList_unpaid_class = "";
$toList_paid_class = "";
$toList_all_class = "";
if($doFlag == "toList_paid"){
	$toList_paid_class = "class='currentLink'";
}else if($doFlag == "toList_all"){
	$toList_all_class = "class='currentLink'";
}else{
	$toList_unpaid_class = "class='currentLink'";
}

?>
<span <?php echo $toList_unpaid_class;?> ><a href="javascript:toList_unpaid()" >未付款</a></span>
<span <?php echo $toList_paid_class;?> ><a href="javascript:toList_paid()">已付款</a></span>
<span <?php echo $toList_all_class;?> ><a href="javascript:toList_all()">全部列表</a></span>
<table >
<thead>
	<tr>
		<th scope="col" width=40>编号</th>	
		<th scope="col" >订单内容</th>
		<th scope="col" width=90>客户名字</th>
		<th scope="col" width=90>客户电话</th>	
		<?php
		if($doFlag == "toList_paid" || $doFlag == "toList_all"){
			echo "<th scope='col' >付款时间</th>";
		}
		?>		
		<th scope="col" width=60>订单状态</th>
		<th scope="col" >进展记录</th>		
	</tr>
</thead>


<tbody>	
<?php
	$commonFunction = new Common_Function();
	if(empty($result) || $result->RecordCount() < 1){
		echo "<tr><td colspan=7 align='center'>记录为空.</td></tr>";
	}else{
			while (!$result->EOF) {
				//取当前任务的进展记录描述
				$sql = "SELECT * from orderprocess where order_id=".$result->fields['id']." order by modifytime asc";	
				$resultProcess = $db->Execute($sql);
				$currentProcess = "";		
				while (!$resultProcess->EOF) {
					$order_processField = $resultProcess->fields['order_process'];
					if($order_processField != "邮件: 催付款"){
						if(strlen($order_processField)>100){
							$order_processField = substr($order_processField,0,100)."...";
						}
						$currentProcess = $currentProcess.$resultProcess->fields['modifytime']."&nbsp;&nbsp;".$order_processField."<br>";
					}
					$resultProcess->MoveNext();	
				}		

				$strTR = "<tr >";		
				$strTR = $strTR."<td width=40>".$result->fields['order_no']."</td>";
				$strTR = $strTR."<td >".$result->fields['products_info']."</td>";
				$strTR = $strTR."<td >".$result->fields['customer_name']."</td>";
				$strTR = $strTR."<td >".$result->fields['customer_telephone']."</td>";
				if($doFlag == "toList_paid" || $doFlag == "toList_all"){
					$strTR = $strTR."<td >".$result->fields['pay_time']."</td>";
				}
				$strTR = $strTR."<td >".$commonFunction->getOrderStatusName($result->fields['order_status'])."</td>";
				$strTR = $strTR."<td>".$currentProcess."<input type='text' size=30 name='process_".$result->fields['id']."' id='process_".$result->fields['id']."'><input type='button' value='提交' onclick='addProcess(".$result->fields['id'].")'></td>";

				$strTR = $strTR."</tr>";
				echo $strTR;
				$result->MoveNext();	
			}
	}

?>

</tbody>
					
</table>	
<br>
<!--Page 分页 -->
<div id="page"><?php echo $page->show(2);?></div>

</div>



</div><!--end container -->
<input type="hidden" name="doFlag" id="doFlag" value="<?php echo $doFlag; ?>" >
<input type="hidden" name="orderId" id="orderId" >
<input type="hidden" name="nowPage" id="nowPage" >
</form>
</body>
</html>

<script>


<?php
$doFlag = $_POST["doFlag"];
if($doFlag == "addProcess"){
	$orderId = $_POST["orderId"];
	echo "$('#process_".$orderId."').focus(); "; 
}
?>

/************翻页跳转**************************/
function goToPage(nowPage){
	var doFlag = $("#doFlag").val();	
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}




/************订单进展**************************/
function addProcess(id){
	document.getElementById('doFlag').value = "addProcess";
	document.getElementById('orderId').value = id;
	var processStr = document.getElementById('process_'+id).value;
	if(processStr == ""){
		alert("进展内容不能为空")
		document.getElementById('process_'+id).focus();  
		return;
	}
	document.listForm.submit();
}

/************状态更新:取消订单**************************/
function doCancel(id){
	document.getElementById('doFlag').value = "doCancel";
	document.getElementById('orderId').value = id;
	document.listForm.submit();
}

/************列表:未付款**************************/
function toList_unpaid(){
	document.getElementById('doFlag').value = "toList_unpaid";
	document.listForm.submit();
}

/************列表:已付款**************************/
function toList_paid(){
	document.getElementById('doFlag').value = "toList_paid";
	document.listForm.submit();
}

/************列表:全部**************************/
function toList_all(){
	document.getElementById('doFlag').value = "toList_all";
	document.listForm.submit();
}
</script>

